﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IXION.BLOG.WEB.UI.UserControl
{
    /// <summary>
    /// 日历控件
    /// </summary>
    public partial class Calendar : System.Web.UI.UserControl
    {
        protected DateTime date;
        protected void Page_Load(object sender, EventArgs e)
        {
            //FillDays();
        }
        //private void FillDays()
        //{
        //    //获取本月的开始和结束日期
        //    DateTime start = new DateTime(date.Year, date.Month, 1);
        //    DateTime end = start.AddMonths(1).AddDays(-1);
        //    string HTML = "<tr>";
        //    HTML += HandleFristDay(start);
        //    for (int i = 1; i <= (int)end.Subtract(start).Days; i++)
        //    {
        //        DateTime currDay = start.AddDays(i);
        //        HTML += "<td>" + (IsToday(currDay) ? "<span class='today'>" + currDay.Day + "</span>" : currDay.Day.ToString()) + "</td>";
        //        if (currDay.DayOfWeek == DayOfWeek.Saturday)
        //        {
        //            HTML += "</tr><tr>";
        //        }
        //    }
        //    DaysLabel.Text = HTML;
        //}

        //private bool IsToday(DateTime p)
        //{
            
        //    return DateTime.Now.ToString("yyyy-MM-dd") == p.ToString("yyyy-MM-dd");
        //    //throw new NotImplementedException();
        //}

        //private string HandleFristDay(DateTime d)
        //{
        //    switch (d.DayOfWeek)
        //    {
        //        case DayOfWeek.Sunday:
        //            return "<td>" + (IsToday(d) ? "<span class='today'>" + d.Day + "</span>" : d.Day.ToString()) + "</td>";
        //        case DayOfWeek.Monday:
        //            return "<td></td><td>" + (IsToday(d) ? "<span class='today'>" + d.Day + "</span>" : d.Day.ToString()) + "</td>";
        //        case DayOfWeek.Tuesday:
        //            return "<td></td><td></td><td>" + (IsToday(d) ? "<span class='today'>" + d.Day + "</span>" : d.Day.ToString()) + "</td>";
        //        case DayOfWeek.Wednesday:
        //            return "<td></td><td></td><td></td><td>" + (IsToday(d) ? "<span class='today'>" + d.Day + "</span>" : d.Day.ToString()) + "</td>";
        //        case DayOfWeek.Thursday:
        //            return "<td></td><td></td><td></td><td></td><td>" + (IsToday(d) ? "<span class='today'>" + d.Day + "</span>" : d.Day.ToString()) + "</td>";
        //        case DayOfWeek.Friday:
        //            return "<td></td><td></td><td></td><td></td><td></td><td>" + (IsToday(d) ? "<span class='today'>" + d.Day + "</span>" : d.Day.ToString()) + "</td>";
        //        case DayOfWeek.Saturday:
        //            return "<td></td><td></td><td></td><td></td><td></td><td></td><td>" + (IsToday(d) ? "<span class='today'>" + d.Day + "</span>" : d.Day.ToString()) + "</td>";
        //        default:
        //            return "";

        //    }
        //    //throw new NotImplementedException();
        //}

        //protected void Unnamed_ServerClick(object sender, EventArgs e)
        //{
        //    date = date.AddMonths(-1);
        //    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('" + date.ToString() + "')", true);
        //    FillDays();
        //}
        //private void Calender1_PreRender(object sender, EventArgs e)
        //{
        //    Thread curr = Thread.CurrentThread;
        //    CultureInfo cc = (CultureInfo)curr.CurrentCulture.Clone();
        //    cc.DateTimeFormat.DayNames = new string[] {"Sun","Mon","Tue","Wen","Thu","Fri","Sat" };
        //    cc.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
        //    curr.CurrentCulture = cc;
        //}
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            DateTime date = Calendar1.SelectedDate;
            Response.Redirect("Search.aspx?key=day&value="+ date.ToString("yyyy-MM-dd"));
        }

        protected void Calendar1_PreRender(object sender, EventArgs e)
        {
            Thread curr = Thread.CurrentThread;
            CultureInfo cc = (CultureInfo)curr.CurrentCulture.Clone();
            cc.DateTimeFormat.DayNames = new string[] { "Sun", "Mon", "Tue", "Wen", "Thu", "Fri", "Sat" };
            cc.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
            curr.CurrentCulture = cc;
        }
    }
}